import { _decorator, Component, director, EditBox, instantiate, Node, UITransform, Vec2 } from 'cc';
import EventManager from '../Global/EventManager';
import { EventEnum, SceneEnum } from '../Enum';
import { NetworkManager } from '../Global/NetworkManager';
import { ApiMsgEnum } from '../Common';
import DataManager from '../Global/DataManager';



const { ccclass, property } = _decorator;

@ccclass('LoginManager')
export class LoginManager extends Component {
    input: EditBox


    onLoad() {
        this.input = this.getComponentInChildren(EditBox)
        director.preloadScene(SceneEnum.Hall)
    }

    async start() {
        await NetworkManager.Instance.connect()
    }
    async handleClick() {


        console.log("handleClick,isConnected", NetworkManager.Instance.isConnected)

        if (!NetworkManager.Instance.isConnected) {
            console.log("未连接")
            await NetworkManager.Instance.connect()
            return
        }


        const nickname = this.input.string
        if (!nickname) {
            console.log("nickname 没有")
            return
        }



        const { success, error, res } = await NetworkManager.Instance.callApi(ApiMsgEnum.ApiPlayerJoin, {
            nickname,
        })
        if (!success) {
            console.log(error)
            return
        }

        //res.player.id


        DataManager.Instance.myPlayerId = res.player.id
        console.log("res", res)
        director.loadScene(SceneEnum.Hall) //登录跳转了，所以一个界面不会有二次机会
    }

}


